from Products.Archetypes.Widget import TypesWidget, registerWidget
from AccessControl import ClassSecurityInfo

class SelectionWidgetPlus(TypesWidget):
    """Similar to a selection widget, but allows for write-ins"""
    
    security = ClassSecurityInfo()
    
    _properties = TypesWidget._properties.copy()
    _properties.update({
        'format': "flex", # possible values: flex, select, radio
        'macro' : "selection_survey_override",
        })

    security.declarePublic('process_form')
    def process_form(self, instance, field, form, empty_marker=None,
                     emptyReturnsMarker=False):
        """process keywords from form where this widget has a list of
        available keywords and a slot for a new one"""
        name = field.getName()
        value = form.get(name, empty_marker)
        if value == '_write-in-option':
            value = form.get('%s_write-in' % name, empty_marker)

        if not value and emptyReturnsMarker: return empty_marker

        return value, {}

registerWidget(SelectionWidgetPlus,
               title='Selection Widget Plus',
               description=('Render a widget to pick from a '
                            'list or possibly a write-in.'),
               used_for=('Products.Archetypes.Field.StringField',)
               )
